import sun.applet.AppletAudioClip;

import javax.swing.*;
import java.applet.AudioClip;

/**
 * @classname 子弹类
 * @Author 姬如千泷
 * @Date 2021/4/29  14:34
 * @Version 1.0
 */
class Mybullet extends Thread{
    MyCharacter ct_label;
    public int bullet_x,bullet_y;
    public boolean dirction;
    public int flag;//判断是子弹还是光波，true为光波
    AudioClip bang_voice = new AppletAudioClip(DateCenter.bang_voiceUrl);
    public Mybullet(int bullet_x, int bullet_y,MyCharacter myCharacter,int flag) {
        this.bullet_x = bullet_x;
        this.bullet_y = bullet_y;
        this.ct_label = myCharacter;
        dirction = ct_label.juage_lr;
        this.flag = flag;
    }
    @Override
    public void run() {
        JLabel bullet = new JLabel();
        if(!ct_label.juage_where)
        ct_label.bg_panel.add(bullet,1);
        else ct_label.myMap.add(bullet,1);
        if(flag==0){//绘制光波
            try {
            for (int i = 0; bullet_y<ct_label.jump_y ;i++) {
                bullet.setBounds(this.bullet_x,this.bullet_y,100,100);
                if(dirction){
                    if(i<3) {bullet.setIcon(juage(i,1));}
                    else bullet.setIcon(juage(3,1));
                    bullet_x-=30;
                    bullet_y+=15;
                }
                else {
                    if(i<3) bullet.setIcon(juage(i,2));
                    else bullet.setIcon(juage(3,2));
                    bullet_x+=30;
                    bullet_y+=15;
                }
                    Thread.sleep(50);
               }
            bang_voice.play();
                for (int i = 0; i < 3; i++) {
                    bullet.setIcon(juage(i,3));
                    Thread.sleep(100);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else if(flag==1){//绘制子弹
            for (int i = 0; bullet_x >-100&&bullet_x<2200 ;i++) {

                bullet.setBounds(this.bullet_x,this.bullet_y,100,100);
                bullet.setIcon(juage(i%6,0));
                if(dirction)
                    bullet_x-=30;
                else bullet_x+=30;
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }else {//绘制白牙特效
            for (int i = 0; i<11;i++) {
                bullet.setLocation(this.bullet_x,this.bullet_y);
                bullet.setSize(500,500);

                if(dirction) {
                    bullet_x -= 60;
                    bullet.setIcon(juage(i,4));
                }
                else {
                    bullet_x+=60;
                    bullet.setIcon(juage(i,5));
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
        if(!ct_label.juage_where) {
            ct_label.bg_panel.remove(bullet);
            ct_label.bg_panel.repaint();
        }else{
            ct_label.myMap.remove(bullet);
            ct_label.myMap.repaint();
        }
    }
    public ImageIcon juage(int k,int l){
        if(l==0){
            switch (k){
                case 0:{return DateCenter.bullet1;}
                case 1:{return DateCenter.bullet2;}
                case 2:{return DateCenter.bullet3;}
                case 3:{return DateCenter.bullet4;}
                case 4:{return DateCenter.bullet5;}
                case 5:{return DateCenter.bullet6;}
                default:return null;
            }
        }else if(l==1){
            switch (k){
                case 0:{return DateCenter.llight0;}
                case 1:{return DateCenter.llight1;}
                case 2:{return DateCenter.llight2;}
                case 3:{return DateCenter.llight3;}
                default:return null;
            }
        }else if(l==2){
            switch (k){
                case 0:{return DateCenter.rlight0;}
                case 1:{return DateCenter.rlight1;}
                case 2:{return DateCenter.rlight2;}
                case 3:{return DateCenter.rlight3;}
                default:return null;
            }
        }else if(l==3){
            switch (k){
                case 0:{return DateCenter.bang1;}
                case 1:{return DateCenter.bang2;}
                case 2:{return DateCenter.bang3;}
                default:return null;
            }
        }else if(l==4){
            switch (k){
                case 0:return DateCenter.lwhiteT1;
                case 1:return DateCenter.lwhiteT2;
                case 2:return DateCenter.lwhiteT3;
                case 3:return DateCenter.lwhiteT4;
                case 4:return DateCenter.lwhiteT4;
                case 5:return DateCenter.lwhiteT4;
                case 6:return DateCenter.lwhiteT4;
                case 7:return DateCenter.lwhiteT5;
                case 8:return DateCenter.lwhiteT6;
                case 9:return DateCenter.lwhiteT7;
                case 10:return DateCenter.lwhiteT8;
                default:return null;
            }
        }else{
            switch (k){
                case 0:return DateCenter.rwhiteT1;
                case 1:return DateCenter.rwhiteT2;
                case 2:return DateCenter.rwhiteT3;
                case 3:return DateCenter.rwhiteT4;
                case 4:return DateCenter.rwhiteT4;
                case 5:return DateCenter.rwhiteT4;
                case 6:return DateCenter.rwhiteT4;
                case 7:return DateCenter.rwhiteT5;
                case 8:return DateCenter.rwhiteT6;
                case 9:return DateCenter.rwhiteT7;
                case 10:return DateCenter.rwhiteT8;
                default:return null;
            }
        }
    }
}
